//
//  JJChatBarEmojiPutView.swift
//  swiftProject
//
//  Created by yangjian on 2021/12/20.
//

import UIKit

//class JJChatBarEmojiTitleItem : UIButton {
//}


class JJChatBarEmojiPutView: UIView {

//    let tableView = UITableView.init(frame: CGRect.zero, style: .plain)
    
    let scrollView = UIScrollView.init()
    let titleItemsStackView = UIStackView.init()
    var titleItems = [UIButton].init()
    var selectedIndex = 0
    //小表情
    let smallEmojiListView = JJSmallEmojiListView.init(frame: CGRect.zero)
    //收藏表情
    let collectEmojiListView = JJBigEmojiListView.init(frame: CGRect.zero)
    //表情组
    var bigEmojiListViews = [JJBigEmojiListView].init()
    
    weak var currentListView : UIView!
    
    func allEmojiViews() -> [UIView] {
        var list = [UIView].init();
        list.append(smallEmojiListView)
        list.append(collectEmojiListView)
        return list + bigEmojiListViews
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    init() {
        super.init(frame: CGRect.zero)
        self.config()
    }
    
    func config(){
        self.backgroundColor = UIColor.white
        self.addSubview(self.scrollView)
        self.scrollView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(40)
        }
        self.titleItemsStackView.axis = .horizontal
        self.titleItemsStackView.alignment = .leading
        self.titleItemsStackView.distribution = .fill
        self.titleItemsStackView.backgroundColor = UIColor.gray
        self.titleItemsStackView.spacing = 8
        self.scrollView.addSubview(self.titleItemsStackView)
        self.titleItemsStackView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(8)
            make.right.equalTo(-8)
            make.bottom.equalTo(0)
            make.height.equalTo(40)
        }
        
        let testCount = 5
        
        for i in 0 ..< 2 + testCount {
            let button = UIButton.init(type: .custom)
            button.tag = i
            button.addTarget(self, action: #selector(titleItemClick(sender:)), for: .touchUpInside)
            button.backgroundColor = UIColor.yellow
            button.setImage(UIImage.init(named: "chatbar_icon_emoji"), for: .normal)
            button.setImage(UIImage.init(named: "test_emoji_s"), for: .selected)
            self.titleItems.append(button)
            self.titleItemsStackView.addArrangedSubview(button)
            button.snp.makeConstraints { make in
                make.height.equalTo(40)
            }
        }
        self.titleItems.first?.isSelected = true

        //小表情
        self.addSubview(smallEmojiListView)
        for i in 0 ..< 209 {
            let model = JJSmallEmojiModel.init()
            model.coverName = String.init(format: "cemoji%03dcover", i);
            model.gifName = String.init(format: "cemoji%03d.gif", i);
            self.smallEmojiListView.models.append(model)
        }
        smallEmojiListView.isHidden = false

        //收藏的大表情
        self.addSubview(collectEmojiListView)
        for imgIndex in 1 ..< 17 {
            let bigEmojiModel = JJBigEmojiModel.init()
            collectEmojiListView.models.append(bigEmojiModel)
            bigEmojiModel.coverName = String.init(format: "icon_%03d_cover", imgIndex)
            bigEmojiModel.emojiName = String.init(format: "icon_%03d.gif", imgIndex)
        }
        collectEmojiListView.isHidden = true
        
        //一组大表情
        for i in 0 ..< testCount {
            let bigEmojiListView = JJBigEmojiListView.init(frame: CGRect.zero)
            self.bigEmojiListViews.append(bigEmojiListView)
            self.addSubview(bigEmojiListView)
            for imgIndex in 1 ..< 17 {
                let bigEmojiModel = JJBigEmojiModel.init()
                bigEmojiListView.models.append(bigEmojiModel)
                bigEmojiModel.coverName = String.init(format: "icon_%03d_cover", imgIndex)
                bigEmojiModel.emojiName = String.init(format: "icon_%03d.gif", imgIndex)
            }
            bigEmojiListView.isHidden = true
        }
        
        for view in allEmojiViews() {
            self.addSubview(view)
            view.snp.makeConstraints { make in
                make.top.equalTo(40)
                make.left.equalTo(0)
                make.bottom.equalTo(0)
                make.right.equalTo(0)
            }
        }
    }
}

extension JJChatBarEmojiPutView {
    
    @objc
    func titleItemClick(sender:UIButton) {
        if sender.tag == self.selectedIndex {return}
        self.titleItems[self.selectedIndex].isSelected = false
        allEmojiViews()[self.selectedIndex].isHidden = true
        self.selectedIndex = sender.tag
        self.titleItems[self.selectedIndex].isSelected = true
        allEmojiViews()[self.selectedIndex].isHidden = false
    }
    
    
}
